﻿using Sodu.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Sodu.Util
{
    public class GetBookEntitykList
    {

        public static List<BookEntity> GetBookEntitykListFromHtml(string html)
        {
            html = html.Replace("\r", "").Replace("\t", "").Replace("\n", "");
            Match t_string = Regex.Match(html, "<form name=\"form2\".*?</form>");

            if (t_string != null && !string.IsNullOrWhiteSpace(t_string.ToString()))
            {
                html = html.Replace(t_string.ToString(), "");
            }
            List<BookEntity> t_list = CommonGetEntityList(html);
            return t_list;
        }

        public static List<BookEntity> GetBookShelfListFromHtml(string html)
        {
            html = html.Replace("\r", "").Replace("\t", "").Replace("\n", "");
            List<BookEntity> t_list = new List<BookEntity>();
            Match readedMatch = Regex.Match(html, "<form name=\"form2\".*?</form>");

            if (readedMatch != null && !string.IsNullOrEmpty(readedMatch.ToString()))
            {
                t_list = CommonGetEntityList(readedMatch.ToString(), true);
            }
            return t_list;
        }

        public static string GetUserNameFromHtml(string html)
        {
            string name = string.Empty;

            html = html.Replace("\r", "").Replace("\t", "").Replace("\n", "");
            Match match = Regex.Match(html, "<a href=\"http://www.soduso.com/myshujia.aspx\" >.*?</a></div>");
            if (match != null && !string.IsNullOrEmpty(match.ToString()))
            {
                name = Regex.Replace(match.ToString(), "<.*?>", "");
            }
            return name;
        }

        public static void GetSearchPageIndexFromHtml(string html, out int prePage, out int nextPage, out int lastPage)
        {
            try
            {
                html = html.Replace("\r", "").Replace("\t", "").Replace("\n", "");
                Match match = Regex.Match(html, "(?<=page=)\\d+(?=\">上一页</a>)");
                prePage = Convert.ToInt32(match.ToString());

                Match match2 = Regex.Match(html, "(?<=page=)\\d+(?=\">下一页</a>)");
                nextPage = Convert.ToInt32(match2.ToString());

                Match match3 = Regex.Match(html, "(?<=page=)\\d+(?=\">尾页</a>)");
                lastPage = Convert.ToInt32(match3.ToString());
            }
            catch (Exception ex)
            {
                prePage = 1;

                nextPage = 1;

                lastPage = 1;
            }
        }

        public static List<BookEntity> CommonGetEntityList(string html, bool flag = false)
        {
            List<BookEntity> t_list = new List<BookEntity>();
            MatchCollection matches = Regex.Matches(html, "<a href=\"/mulu.*?>.*?</div></div>");
            if (matches.Count == 0)
            {
                t_list = null;
                return t_list;
            }

            BookEntity t_entity;
            for (int i = 0; i < matches.Count; i++)
            {
                t_entity = new BookEntity();
                string t_string = "<div>" + matches[i].ToString();
                try
                {
                    MatchCollection matches2 = Regex.Matches(t_string, "<div.*?>.*?</div>");
                    if (matches.Count == 0)
                    {
                        continue;
                    }
                    t_entity.BookName = Regex.Replace(matches2[0].ToString(), "<.*?>", "");
                    t_entity.ContentsUrl = Regex.Match(matches2[0].ToString(), "(?<=<a href=\").*?(?=\">)").ToString();

                    t_entity.ChapterName = Regex.Replace(matches2[1].ToString(), "<.*?>", "");

                    Match str_Unread = Regex.Match(t_entity.ChapterName, @"\(未读.*?\)");
                    if (str_Unread != null && !string.IsNullOrEmpty(str_Unread.ToString()))
                    {
                        t_entity.UnReadCountData = str_Unread.ToString();
                        t_entity.ChapterName = t_entity.ChapterName.Replace(t_entity.UnReadCountData, "");

                    }
                    t_entity.UpdateTime = Regex.Replace(matches2[2].ToString(), "<.*?>", "");
                    t_entity.Bookshelf = flag;
                    t_list.Add(t_entity);
                }
                catch
                {
                    t_list = null;
                    return t_list;
                }

            }
            return t_list;
        }

    }
}
